package com.study.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.study.entity.User;
import com.study.service.IUserService;
import com.study.util.MySqlDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;

public class UserServiceImpl implements IUserService {

    private static final Log log = LogFactory.get();

    @Override
    public int registerUser(User user) {

        int row = 0;
        MySqlDataSource mySqlDataSource = new MySqlDataSource();
        Connection connection = mySqlDataSource.getConnection();
        String sql = "insert into user (user_no,user_name,user_mobile,user_password,create_time) values(?,?,?,?,?)";
        PreparedStatement preparedStatement = null;

        try{
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUserNo());
            preparedStatement.setString(2,user.getUserName());
            preparedStatement.setString(3,user.getUserMobile());
            preparedStatement.setString(4,user.getUserPassword());
            preparedStatement.setString(5, DateUtil.now());

            row = preparedStatement.executeUpdate();

            //关闭链接
            preparedStatement.close();
            connection.close();

        }catch (Exception e){
            log.info("SQL语句执行报错[{}]",sql);
        }

        return row;
    }

    @Override
    public User selectUserByUserName(String loginName) {

        User user = null;
        MySqlDataSource mySqlDataSource = new MySqlDataSource();
        Connection connection = mySqlDataSource.getConnection();
        String sql = "select * from user where user_name = ?";
        PreparedStatement preparedStatement = null;

        try{
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,loginName);
            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String userNo = resultSet.getString("user_no");
                String userName = resultSet.getString("user_name");
                String userMobile = resultSet.getString("user_mobile");
                String userPassword = resultSet.getString("user_password");
                Date createTime = resultSet.getDate("create_time");

                user = new User();
                user.setId(id);
                user.setUserNo(userNo);
                user.setUserName(userName);
                user.setUserMobile(userMobile);
                user.setUserPassword(userPassword);
                user.setCreateTime(createTime);
            }

        }catch (Exception e){
            log.info("SQL语句执行报错[{}]",sql);
        }

        return user;
    }

    @Override
    public int updateUserPassword(User user) {
        int row = 0;
        MySqlDataSource mySqlDataSource = new MySqlDataSource();
        Connection connection = mySqlDataSource.getConnection();
        String sql = "update user set user_name=?,user_mobile=?,user_password=? where id=?";
        PreparedStatement preparedStatement = null;

        try{
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUserName());
            preparedStatement.setString(2,user.getUserMobile());
            preparedStatement.setString(3,user.getUserPassword());
            preparedStatement.setInt(4, user.getId());

            row = preparedStatement.executeUpdate();

            //关闭链接
            preparedStatement.close();
            connection.close();

        }catch (Exception e){
            log.info("SQL语句执行报错[{}]",sql);
        }

        return row;
    }

    @Override
    public int deleteUserById(Integer id) {
        int row = 0;
        MySqlDataSource mySqlDataSource = new MySqlDataSource();
        Connection connection = mySqlDataSource.getConnection();
        String sql = "delete from user where id=?";
        PreparedStatement preparedStatement = null;

        try{
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);

            row = preparedStatement.executeUpdate();

            //关闭链接
            preparedStatement.close();
            connection.close();

        }catch (Exception e){
            log.info("SQL语句执行报错[{}]",sql);
        }

        return row;
    }

    @Override
    public int selectMaxId() {
        int maxId = 0;
        MySqlDataSource mySqlDataSource = new MySqlDataSource();
        Connection connection = mySqlDataSource.getConnection();
        String sql = "select max(id) as 'id' from user";
        PreparedStatement preparedStatement = null;

        try{
            preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()){
                int id = resultSet.getInt("id");
                maxId = id + 1;
            }

        }catch (Exception e){
            log.info("SQL语句执行报错[{}]",sql);
        }

        return maxId;
    }

}
